package kr.ac.hufs.ice.cap.network;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import kr.ac.hufs.ice.cap.network.NetworkData;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

public class NetworkThread extends Thread {
	
	NetworkData nd;
	Handler mHandler;
	int commandState; 				
	private final String SERVER_ADDRESS = "http://220.67.124.69/parentinfo/insert.php"; 
	
	public static final int REGISTER = 0;
	
	public NetworkThread(NetworkData nd, Handler mHandler) {
		this.nd = nd;
		this.mHandler = mHandler;
	}
	
	public void setCommand(int command) {
		this.commandState = command;
	}
	
	public void run() {
		Looper.prepare();
		if(commandState == REGISTER) {
			register();	
		}
		
		Looper.loop();
	}
	
	private void register() {
		
		String Id = nd.getId();
		String PassWord = nd.getPassWord();
		String ParentName = nd.getParentName();
		String ParentPhoneNumber = nd.getParentPhoneNumber();
		
		PageStreamer ps = new PageStreamer();
		ps.setRootURL(SERVER_ADDRESS);
		ps.add("Id", Id);
		ps.add("PassWord", PassWord);
		ps.add("ParentName", ParentName);
		ps.add("ParentPhoneNumber", ParentPhoneNumber);
		
		String returnMsg = ps.getPageStream();
		Message msg = new Message();
		msg.what = NetworkThread.REGISTER;
		if(returnMsg.contains("success")) {
			msg.obj = "success";
			mHandler.sendMessage(msg);
		}else {
			msg.obj = "fail";
			mHandler.sendMessage(msg);
		}
		
	}
}
